package com.ruiboyun.liveapp.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.base.BaseAdapter;
import com.ruiboyun.liveapp.base.ViewHolder;
import com.ruiboyun.liveapp.bean.Review;
import com.ruiboyun.liveapp.bean.Share;
import com.ruiboyun.liveapp.util.tool.DateUtils;
import com.ruiboyun.liveapp.util.tool.ImageUtils;

import java.util.List;

/**
 * Created by wushiqian on 2017/9/19.
 */

public class ReviewAdapter extends BaseAdapter<Review> {
    public ReviewAdapter(Context context, List<Review> list) {
        super(context, list);
    }
    private OnReviewListener onReviewListener;

    public void setOnReviewListener(OnReviewListener onReviewListener) {
        this.onReviewListener = onReviewListener;
    }

    public interface OnReviewListener{
        void onReview(Review review);
        void onShare(View view,Review review);
    }

    @Override
    public View getLayoutView(ViewGroup parent, int viewType) {
        return getView(parent, R.layout.recyclerview_item_review);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Review review = list.get(position);
        ImageUtils.load(context, review.getCover_addr(), holder.getImageView(R.id.img));
        holder.getTextView(R.id.title).setText(review.getTitle());
        holder.getTextView(R.id.start_time).setText("\t"+DateUtils.long2StrDate(review.getStart_time()));
        long duration = review.getDuration();
        long second = duration % 60;
        long minute = duration / 60 % 60;
        long hour = duration / 3600;
        if (hour > 0) {
            holder.getTextView(R.id.total_time).setText("\t"+hour + "小时" + getString(minute) + "分" + getString(second) + "秒");
        } else if (hour == 0 && minute > 0) {
            holder.getTextView(R.id.total_time).setText("\t"+minute + "分" + getString(second) + "秒");
        } else if (minute == 0) {
            holder.getTextView(R.id.total_time).setText("\t"+getString(second) + "秒");
        }

        holder.getTextView(R.id.see_num).setText(review.getView_times()+"次 观看");
        holder.getImageView(R.id.play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onReviewListener!=null){
                    onReviewListener.onReview(review);
                }
            }
        });
        holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onReviewListener!=null){
                    onReviewListener.onReview(review);
                }
            }
        });
        holder.getImageView(R.id.share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onReviewListener!=null){
                    onReviewListener.onShare(v,review);
                }
            }
        });
    }

    public String getString(long time) {
        String str = "00";
        if (time > 9) {
            str = String.valueOf(time);
        } else {
            str = "0" + time;
        }
        return str;
    }
}
